//
//  MenuViewController.m
//  Menu
//
//  Created by YR on 2017/5/31.
//  Copyright © 2017年 YR. All rights reserved.
//

#import "MenuViewController.h"
#import "ContentViewController.h"
#import "Test1ViewController.h"
#import "Test2ViewController.h"
#import "MenuView.h"

@interface MenuViewController ()

@property (nonatomic) MenuView *menuView;
@property (nonatomic) ContentViewController *contenViewController;

@end

@implementation MenuViewController
+ (instancetype)shareInstance {
    static MenuViewController *menuVC;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        menuVC = [[MenuViewController alloc] init];
    });
    
    return menuVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.menuView];
    self.contenViewController = [[ContentViewController alloc] init];
    [self addChildViewController:self.contenViewController];
    [self.view addSubview:self.contenViewController.view];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    [self.contenViewController.view addGestureRecognizer:pan];
}

- (void)panGesture:(UIPanGestureRecognizer *)recognizer {
    CGPoint point = [recognizer translationInView:self.contenViewController.view];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + point.x, recognizer.view.center.y);
    CGFloat viewX = recognizer.view.frame.origin.x;
    CGFloat viewY = recognizer.view.frame.origin.y;
    
    if (viewX < 0) {
        recognizer.view.frame = CGRectMake(0, viewY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
    } else if (viewX >= self.view.frame.size.width * 0.8) {
        
        recognizer.view.frame = CGRectMake(self.view.frame.size.width * 0.8, viewY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
    } else {
        recognizer.view.frame = CGRectMake(viewX + point.x, viewY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
        
    }
    
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        
        
        if (viewX < 120) {
            [UIView animateWithDuration:0.5 animations:^{
                recognizer.view.frame = CGRectMake(0, viewY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
            }];
            
        }
        if (viewX > 120) {
            [UIView animateWithDuration:0.5 animations:^{
                recognizer.view.frame = CGRectMake(self.view.frame.size.width * 0.8, viewY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
            }];
        }
    }
    
    [recognizer setTranslation:CGPointZero inView:self.contenViewController.view];
}

- (MenuView *)menuView {
    if (!_menuView) {
        _menuView = [[MenuView alloc] initWithFrame:self.view.frame];
        _menuView.backgroundColor = [UIColor grayColor];
        
        __weak typeof(self) weakself = self;
        _menuView.tableViewSelectIndexBlock = ^(NSInteger index) {
            
            if (index == 0) {
                Test1ViewController *test1 = [[Test1ViewController alloc] init];
                [weakself.navigationController pushViewController:test1 animated:YES];
            } else if (index == 1) {
                Test2ViewController *test2 = [[Test2ViewController alloc] init];
                [weakself.navigationController pushViewController:test2 animated:YES];
            }
        };
    }
    return _menuView;
}

- (void)show {
    if (_contenViewController.view.frame.origin.x < 10) {
        [UIView animateWithDuration:0.5 animations:^{
            _contenViewController.view.frame = CGRectMake(self.view.frame.size.width * 0.8, _contenViewController.view.frame.origin.y, _contenViewController.view.frame.size.width, _contenViewController.view.frame.size.height);
        }];

    } else {
        [UIView animateWithDuration:0.5 animations:^{
            _contenViewController.view.frame = CGRectMake(0, _contenViewController.view.frame.origin.y, _contenViewController.view.frame.size.width, _contenViewController.view.frame.size.height);
        }];

    }
}

@end
